@extends('admin.layout')

@section('title', $title)

@section('content')
		
	
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <section class="content-header">
            <h1>
                订单管理
                <small>订单列表</small>
            </h1>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
                <li><a href="#">订单管理</a></li>
                <li class="active">订单列表</li>
            </ol>
        </section>

        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-xs-12">
                    <div class="box">
                            <div class="box-header" >
                                <h3 class="box-title">查看订单</h3>
                            </div><!-- /.box-header -->
							@if (session('info'))
								<div id='error'>                        
									<div class="alert alert-danger">         
			                             <p>{{ session('info') }}</p>
                                    </div>
		                        </div>
			                @endif
						
                        <div class="box-body">

                            <div class='col-md-9'>
                                <h6 class="btn btn-primary" value='5' >全部订单</h6>
                                <h6 class="btn" value='0'>待付款</h6>
                                <h6 class="btn" value='1'>待发货</h6>
                                <h6 class="btn" value='2'>待收货</h6>
                                <h6 class="btn" value='3'>已完成</h6>
                                <h6 class="btn" value='6'>申请退款</h6>
                                <h6 class="btn" value='7'>已退款</h6>

                            </div>
                        <form action="{{ url('admin/order/index') }}" >
                            <div class='col-md-3' style="padding-top:10px;">
                                <div class="input-group input-group-sm" >
                                    <input value="{{ $request['keywords'] or '' }}" name='keywords' class="form-control" type="text" >
                                    <span class="input-group-btn">
                                        <button class="btn btn-info btn-flat">搜索</button>
                                    </span>
                                </div>
                            </div>
                                
                        </form>
                        </div>
                        <div class='box-body'>
                        </div>
                            <div class='box-body' id="div">
                        
                            <table id="example2" class="table table-bordered table-hover">
                               
                            </table>

                        </div>
                             
                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                  

                </div><!-- /.col -->
            </div><!-- /.row -->
        </section><!-- /.content -->
    </div><!-- /.content-wrapper -->
    <script type="text/javascript">

        window.onload = function()
        {
            $('.mouse').css('cursor', 'pointer');

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                 }
            });

           //单击事件
           $('h6').on('click',function(){

                ajaxOn($(this));

            });//button 单击

            //进入列表页先执行一遍,显示全部订单
            ajaxOn($('h6').eq(0));

            // console.log($('h6'));

            //封装函数
            function ajaxOn(btn)
            {
                //获取点击的第i个按钮的索引
                var i =btn.index(); 

                // 点击button修改class样式  
                btn.addClass('btn-primary');

                //同时兄弟节点失去样式
                btn.siblings().removeClass('btn-primary');

                //单击将val值取出
                var val = btn.attr('value');

                //ajax实现分类
                $.ajax({
                    type:'POST',
                    url:"{{ url('admin/order/ajaxPay') }}",
                    data:{status:val},
                    dataType:'json',
                    success:function(data)
                    {
                       
                       if(data == null || data == '')
                       {
                            $('#example2').html("<h5>没有相应条件的信息</h5>");
                       }
                       
                        var item = '<tr><th>编号</th><th>订单号</th><th>买家</th><th>买家手机号</th><th>下单时间</th><th>支付方式</th><th>订单状态</th><th>订单详情</th></tr>';

                        $(data).each(function(i,result){
                            
                            //将时间戳转化为00-00-00 00:00:00格式
                            function getDate(tm){
                                var time=new Date(parseInt(tm) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ")
                                return time;
                            }

                            var addtime = getDate(result.addtime);
                            
                            //向表格中添加单元格
                            var status = '';
                            var method = '';

                            if(result.post_status == 0)
                            {
                                status = '待付款';
                            }else if(result.post_status == 1)
                            {
                                status = "<a>待发货</a>";

                            }else if(result.post_status == 2)
                            {
                                status = '待收货';
                            }else if(result.post_status == 3)
                            {
                                status = '已完成';
                            }else if(result.post_status == 6)
                            {
                                status = '<a>申请退款</a>';
                            }else if(result.post_status == 7)
                            {
                                status = '已退款';
                            }

                            if(result.post_method == 0)
                            {
                                method = '在线支付';
                            }

                            item += "<tr><td id='id'>"+result.id+"</td><td>"+result.post_number+"</td><td>"+result.nickname+"</td><td>"+result.phone+"</td><td>"+addtime+"</td><td>"+method+"</td><td class='post_status' oid = "+result.id+">"+status+"</td><td><a href={{ url('/admin/order/orderdetail') }}/"+result.member_id+"/"+result.goods_id+">查看</a></td></tr>";

                            $('#example2').html(item);

                        //获取改变发货状态的id
                        // var id = result.id;
                        $('.post_status').css('cursor', 'pointer');
                        //单击实现发货
                        $('.post_status').on('click', function(){

                            var c = $(this);
                            var id = c.attr('oid');
                            $.ajax({
                                type:'POST',
                                url:"{{ url('admin/order/ajaxSend') }}",
                                data:{id:id},
                                success:function(data)
                                {
                                    if(data == 2)
                                    {
                                        c.html('待收货');
                                    }else if(data == 7)
                                    {
                                        c.html('已退款');
                                    }
                                },
                                error:function()
                                {
                                    alert('异常');
                                }

                            });

                            c.unbind('click');

                        });//status ajax

                       
                    });//遍历json数组

                        
                   }//success

                });//ajax 分类

            
            }  
        }   

        

    </script>

@endsection